package com.EAIPClub.CodeShellDebugDataGetter.service.impl;

import com.EAIPClub.CodeShellDebugDataGetter.service.CodeShellDebugDataGetterService;
import com.api.POJO.CodeLab.CodeShell.CodeShellDebugReturnBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.*;
/**
 * @Description :
 * @Author :
 * @Date : 2021/12/19
 */

@Service
@Slf4j
public class CodeShellDebugDataGetterServiceImpl implements CodeShellDebugDataGetterService {

    @Override
    public CodeShellDebugReturnBean getData(String filename) throws IOException {
        String data = readFile(filename);
        log.info(data);
        String[] dump_arrs = data.split("arr = \\{")[1].split("\n")[1].split(",");
        for(int i =1;i<dump_arrs.length;i++){
            String temp = dump_arrs[i].split(" ")[1];
            dump_arrs[i]=temp;
        }
        return new CodeShellDebugReturnBean(dump_arrs);
    }

    private String readFile(String filename) throws IOException {
        String path = "/home/student/"+filename;
        File tempFile = new File(path);
        StringBuilder buffer = new StringBuilder();
        BufferedReader bf= new BufferedReader(new FileReader(path));
        String s;
        while((s = bf.readLine())!=null){//使用readLine方法，一次读一行
            buffer.append(s.trim()).append("\n");
        }
        FileWriter fileWriter =new FileWriter(tempFile);
        fileWriter.write("");
        fileWriter.flush();
        fileWriter.close();
        return buffer.toString();
    }
}
